package com.kgc.dubbo.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.kgc.dubbo.entity.QgUser;
import com.kgc.dubbo.exception.UserException;
import com.kgc.dubbo.service.LocalUserService;
import com.kgc.dubbo.service.QgUserService;
import com.kgc.dubbo.utils.Constants;
import com.kgc.dubbo.utils.DateTools;
import com.kgc.dubbo.utils.EmptyUtils;
import com.kgc.dubbo.utils.MD5;
import com.kgc.dubbo.utils.compoent.RedisTools;
import com.kgc.dubbo.vo.RespResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 本地的业务实现类
 */
@Service
public class LocalUserServiceImpl implements LocalUserService {

    //@Reference
    private QgUserService qgUserService;
    @Autowired
    private RedisTools redisTools;

    @Override
    public RespResult login(String phone, String password) throws Exception {
        //以token:phone为key去redis中查询该用户是否已经登录
        String token = redisTools.getObject(Constants.TOKEN_PREFIX+phone,String.class);
        if (EmptyUtils.isNotEmpty(token)){//不为空说明用户已经登录
            //重新设置过期时间
            //删除redis中的key:token:phone
            redisTools.del(Constants.TOKEN_PREFIX+phone);
            //删除redis中的key:token
            redisTools.del(token);
            //return RespResult.ok(token);
        }
        Map<String,Object> params = new HashMap<>();
        params.put("phone",phone);
        params.put("password",password);
        QgUser qgUser = qgUserService.getQgUserByLogin(params);
        if(qgUser==null){//登录失败
            return RespResult.build(UserException.USER_PASSWORD_ERROR.getCode(),
                    UserException.USER_PASSWORD_ERROR.getMassage());
        }
        //说明登录成功
        //1生成token
        token = this.generateToken(qgUser);
        //2以生成token为key，用户的登录信息问值存入redis中
        qgUser.setPassword(null);
        //以token:phone为key,token为值存储到redis中
        redisTools.set(Constants.TOKEN_PREFIX+qgUser.getPhone(),token,Constants.TOKEN_EXPIRE_TIME);
        //以token为key,token为值存储到redis中
        redisTools.set(token,qgUser,Constants.TOKEN_EXPIRE_TIME);
        //3返回token
        return RespResult.ok(token);
    }

    @Override
    public RespResult logOut(String token) throws Exception {
        QgUser qgUser = redisTools.getObject(token,QgUser.class);
        //代码能走到这里说明根据token能查询到用户的信息
        //删除用户的信息以及token
        redisTools.del(token,Constants.TOKEN_PREFIX+qgUser.getPhone());
        return RespResult.ok();
    }

    private String generateToken(QgUser user) throws ParseException {
        //生成token的规则：id+phone+系统当前时间+6位随机数+md5加密生成的一个16位数的字符串
        StringBuffer tokenSB = new StringBuffer(Constants.TOKEN_PREFIX+user.getId()+user.getPhone());
        String dateStr = DateTools.format(new Date(), "yyyyMMddHHmmss");
        tokenSB.append(dateStr);
        tokenSB.append(Math.random()*100000);
        String token = MD5.getMd5(tokenSB.toString(), 16);
        token = Constants.TOKEN_PREFIX+token;
        return token;
    }
}
